package cn.team11.smarteye.impl;

import android.content.Intent;
import android.os.SystemClock;

import org.apache.commons.lang3.StringUtils;
import org.greenrobot.greendao.annotation.Index;

import cn.team11.smarteye.activity.VideoCallActivity;
import cn.team11.smarteye.bean.CateyeInfoBean;
import cn.team11.smarteye.contract.DeviceDetailContract;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.module.MyDaoManager;
import cn.team11.smarteye.service.CatEyeService;
import cn.team11.smarteye.utils.AppLogger;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Murphy on 2017/10/19.
 */

public class DeviceDetailPresenterImpl implements DeviceDetailContract.Presenter {

    private DeviceDetailContract.View mView;
    private CateyeInfoBean mDeviceInfo;
    private String mName;

    public DeviceDetailPresenterImpl(DeviceDetailContract.View mView, String name) {
        this.mView = mView;
        this.mName = name;
        mView.setPresenter(this);
    }

    @Override
    public void start() {
        init(mName);
    }

    @Override
    public void destroy() {
        //更新数据库

    }

    private void update(){
        Observable
                .just(mDeviceInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new Consumer<CateyeInfoBean>() {
                    @Override
                    public void accept(CateyeInfoBean infoBean) throws Exception {
                        MyDaoManager.getInstance(mView.getContext()).updateCateyeInfo(infoBean);
                    }
                });
    }

    private void init(final String name){
        Observable.just(name).map(new Function<String, CateyeInfoBean>() {
            @Override
            public CateyeInfoBean apply(@NonNull String s) throws Exception {
                return MyDaoManager.getInstance(mView.getContext()).getCateyeInfo(name);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Consumer<CateyeInfoBean>() {
                    @Override
                    public void accept(CateyeInfoBean infoBean) throws Exception {
                        mDeviceInfo = infoBean;
                        //先读取最近一个月内的报警信息列表和门铃列表，并更新到本地数据库中
                        long endtime = System.currentTimeMillis();
                        long starttime = endtime - SConstants.EVERYDAY_MILLS_TIME * SConstants.MONTH_DAYS;
                        getAlarmMsgList(starttime,endtime,SConstants.MONTH_LIMIT);
                        getGuestRecordList(starttime, endtime, SConstants.MONTH_LIMIT);
                    }
                });
    }

    @Override
    public boolean getDeviceInfo() {

        if (CatEyeService.icvss != null && mDeviceInfo != null){
            if (StringUtils.isEmpty(mDeviceInfo.getUid())) return false;
            CatEyeService.icvss.equesGetDeviceInfo(mDeviceInfo.getUid());
            return true;
        }
        return false;
    }

    @Override
    public void enablePirSwitch(boolean value) {
        if (CatEyeService.icvss != null){
            if (value){
                CatEyeService.icvss.equesSetPirEnable(mDeviceInfo.getUid(), 1);
            } else {
                CatEyeService.icvss.equesSetPirEnable(mDeviceInfo.getUid(), 0);
            }

        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }
    }

    @Override
    public void restartDevice() {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesRestartDevice(mDeviceInfo.getUid());
        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }
    }

    @Override
    public void deleteDevice() {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesDelDevice(mDeviceInfo.getBid());
            Observable.just(mDeviceInfo)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.io())
                    .subscribe(new Consumer<CateyeInfoBean>() {
                        @Override
                        public void accept(CateyeInfoBean infoBean) throws Exception {
                            MyDaoManager.getInstance(mView.getContext()).deleteCateyeInfo(infoBean);
                        }
                    });
        }
    }

    @Override
    public void renameDevice(String nick) {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesSetDeviceNick(mDeviceInfo.getBid(), nick);
        }
    }

    @Override
    public void getPirInfo() {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesGetDevicePirInfo(mDeviceInfo.getUid());
        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }
    }

    @Override
    public void getAlarmMsgList(long starttime, long endtime, int limit) {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesGetAlarmMessageList(mDeviceInfo.getBid(), starttime, endtime, limit);
        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }
    }


    @Override
    public void delAlarmMsg(String[] fids, int delall) {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesDelAlarmMessage(mDeviceInfo.getBid(), fids, delall);
        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }

    }

    @Override
    public void getGuestRecordList(long starttime, long endtime, int limit) {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesGetRingRecordList(mDeviceInfo.getBid(), starttime, endtime, limit);
        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }

    }

    @Override
    public void delGuestRecord(String[] fids, int delall) {
        if (CatEyeService.icvss != null){
            CatEyeService.icvss.equesDelRingRecord(mDeviceInfo.getBid(), fids, delall);
        } else {
            AppLogger.e("ICCVSSUserInstance is null!!!");
        }

    }

    @Override
    public void startVideoCall() {
        Intent intent = new Intent(mView.getContext(), VideoCallActivity.class);
        intent.putExtra(SConstants.NAME, mDeviceInfo.getName());
        intent.putExtra(SConstants.NICKNAME, mDeviceInfo.getNick());
        intent.putExtra(SConstants.UID,mDeviceInfo.getUid());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mView.getContext().startActivity(intent);
    }

}
